using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroEquip : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_HeroEquip
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject equipCell { protected set; get; }
            public UnityEngine.GameObject wearEquipsInfo { protected set; get; }
            public UnityEngine.GameObject petCell { protected set; get; }
            public UnityEngine.GameObject wearPetInfo { protected set; get; }
            public UnityEngine.GameObject emptyPet { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnClear { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnAutoEquip { protected set; get; }
            public UnityEngine.GameObject selectedEquip { protected set; get; }
            public UnityEngine.GameObject selectedPet { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("equipCell", out var __tbv0);
                this.equipCell = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("wearEquipsInfo", out var __tbv1);
                this.wearEquipsInfo = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("petCell", out var __tbv2);
                this.petCell = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("wearPetInfo", out var __tbv3);
                this.wearPetInfo = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("emptyPet", out var __tbv4);
                this.emptyPet = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClear", out var __tbv5);
                this.btnClear = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnAutoEquip", out var __tbv6);
                this.btnAutoEquip = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("selectedEquip", out var __tbv7);
                this.selectedEquip = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("selectedPet", out var __tbv8);
                this.selectedPet = __tbv8;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private UIN_HeroEquipWearEquipsInfo wearsEquipsInfo = null;
        private ListScrollAdapter<UIN_HeroEquipCell> equipSlots = null;
        private UIN_HeroEquipPetCell petSlot = null;
        private UIN_HeroEquipSelectedInfo equipSelectedInfo = null;
        private UIN_HeroEquipWearPetInfo wearPetInfo = null;
        private UIN_HeroEquipSelectedPetInfo petSelectedInfo = null;

        private HeroData heroData = null;
        private int indexEquipColumn = -1;
        #endregion fields

        #region properties

        protected UIB_UIN_HeroEquip ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroEquip();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.equipSlots = this.InitListScroll<UIN_HeroEquipCell>(this.ui.equipCell, this.OnEquipSlotChanged);
            this.equipSlots.InitUnitSelected(UnitSelectType.ByExButton, this.OnEquipSlotSelected);
            this.equipSelectedInfo = this.InitChildNode<UIN_HeroEquipSelectedInfo>(this.ui.selectedEquip);
            this.wearsEquipsInfo = this.InitChildNode<UIN_HeroEquipWearEquipsInfo>(this.ui.wearEquipsInfo);

            this.petSlot = this.InitChildNode<UIN_HeroEquipPetCell>(this.ui.petCell);
            this.petSelectedInfo = this.InitChildNode<UIN_HeroEquipSelectedPetInfo>(this.ui.selectedPet);
            this.wearPetInfo = this.InitChildNode<UIN_HeroEquipWearPetInfo>(this.ui.wearPetInfo);

            this.ui.btnAutoEquip.SetActive(false);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            this.HideEquipSelectedInfo();
            this.HidePetSelectedInfo();
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            // this.ui.btnAutoEquip.onClick.AddListener(this.OnClickAutoEquip);
            this.ui.btnClear.onClick.AddListener(this.OnClickClear);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        private void OnClickAutoEquip()
        {
        }

        private void OnClickClear()
        {
            bool isWear = this.heroData.IsWearEquip() || this.heroData.IsWearPet();
            if (!isWear)
            {
                NoticeModule.ShowNoticeNoCall(APIUIHeroInfo.NoticeCid_TakeOffAllWhenNoWears);
                return;
            }

            NoticeModule.ShowNotice(APIUIHeroInfo.NoticeCid_TakeOffAllEquipsAndPets, this.CallSureClear);
        }

        private void CallSureClear()
        {
            this.SendClearHeroWearEquipsAndPet().Forget();
        }

        private async UniTaskVoid SendClearHeroWearEquipsAndPet()
        {
            var result = await HeroModule.NetCore.CS_clearEquip(this.heroData.cid);
            if (result.IsSuccess)
            {
                HeroModule.Instance.UpHeroByPOD(result.heroPOD);
                if (this.isActive)
                {
                    this.ShowAll();
                }
            }
        }

        public void Show(HeroData data, int indexEquipColumn)
        {
            this.Show();
            this.SetData(data, indexEquipColumn);
            this.ShowAll();
        }

        private void ClearData()
        {
            this.SetData(null, -1);
        }

        private void SetData(HeroData data, int indexEquipColumn)
        {
            this.heroData = data;
            this.SetEquipSlotSelectedIndex(indexEquipColumn);
        }

        private void SetEquipSlotSelectedIndex(int index)
        {
            this.indexEquipColumn = index;
        }

        private void ShowAll()
        {
            this.ShowWearEquipsInfo();
            this.ShowHeroEquipSlots();
            this.ShowHeroPet();
        }

        private void ShowWearEquipsInfo()
        {
            this.wearsEquipsInfo.Show(this.heroData);
        }

        private void ShowHeroEquipSlots()
        {
            this.equipSlots.RefillCells(APIUIHeroInfo.MaxHeroEquipColumn, this.indexEquipColumn);
        }

        private void OnEquipSlotChanged(UIN_HeroEquipCell item, int index)
        {
            item.Show(this.heroData, index);
            item.SetSelected(index == this.indexEquipColumn);
        }

        private void OnEquipSlotSelected(int index, UIN_HeroEquipCell item)
        {
            this.ShowEquipSelectedInfo(index, -1);
        }

        private void ShowEquipSelectedInfo(int index, long selectedEquipId)
        {
            int column = index + 1;
            if (column == 0)
            {
                this.HideEquipSelectedInfo();
                return;
            }
            int limitLv = APIUIHeroInfo.LimitLvUnlockHeroEquipColumns[index];
            if (this.heroData.level < limitLv)
            {
                NoticeModule.ShowNoticeNoCall(APIUIHeroInfo.NoticeCid_UnlockByHeroLevel, limitLv.ToString());
                this.HideEquipSelectedInfo();
                return;
            }
            this.UnSelectedLastEquipSlotState();
            this.HidePetSelectedInfo();
            this.SetEquipSlotSelectedIndex(index);
            var equipColumnItem = this.equipSlots.GetTEntity(this.indexEquipColumn);
            if (equipColumnItem != null)
            {
                equipColumnItem.SetSelected(true);
            }
            this.equipSelectedInfo.Show(this.heroData, column, selectedEquipId);
        }

        private void UnSelectedLastEquipSlotState()
        {
            var lastEquipColumnItem = this.equipSlots.GetTEntity(this.indexEquipColumn);
            if (lastEquipColumnItem != null)
            {
                lastEquipColumnItem.SetSelected(false);
            }
        }

        private void HideEquipSelectedInfo()
        {
            this.equipSelectedInfo?.Hide();
        }

        private void ShowHeroPet()
        {
            this.ShowHeroPetCell();
            GameDataCenter.PlayerData.pet.TryGetValue(this.heroData.petId, out var pet);
            this.ShowHeroPetSkillAndAttributes(pet);

            this.ui.emptyPet.SetActive(pet == null);
        }

        private void ShowHeroPetCell()
        {
            this.petSlot.Show(this.heroData, this.OnClickPet);
        }

        private void OnClickPet()
        {
            this.ShowPetSelectInfo();
        }

        private void ShowPetSelectInfo()
        {
            int limitLv = APIUIHeroInfo.LimitLvUnlockHeroPetColumn;
            if (this.heroData.level < limitLv)
            {
                NoticeModule.ShowNoticeNoCall(APIUIHeroInfo.NoticeCid_UnlockByHeroLevel, limitLv.ToString());
                return;
            }

            this.UnSelectedLastEquipSlotState();
            this.SetEquipSlotSelectedIndex(-1);
            this.HideEquipSelectedInfo();
            this.petSelectedInfo.Show(this.heroData);
        }

        private void HidePetSelectedInfo()
        {
            this.petSelectedInfo?.Hide();
        }

        private void ShowHeroPetSkillAndAttributes(Pet pet)
        {
            this.wearPetInfo.Show(pet);
        }

        public void RefreshCurrEquipView()
        {
            this.ShowWearEquipsInfo();
            this.equipSlots.selectedType = UnitSelectType.None;
            this.ShowHeroEquipSlots();
            this.equipSlots.selectedType = UnitSelectType.ByExButton;
        }

        public void RefreshWhenCloseSelectedPanel()
        {
            this.UnSelectedLastEquipSlotState();
            this.ShowHeroPet();
        }

        public void RefreshPet()
        {
            this.ShowHeroPet();
        }

        #endregion methonds
    }
}
